import 'dart:convert';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
// import 'package:hex/hex.dart';
import 'dart:convert';

import 'package:df/utils/logger_utils.dart';
import 'package:intl/intl.dart';

class Sms {
  final String? countryCode;
  final String? phoneNum;
  final String? appID;
  final String? sendTime;
  final String? token;
  // final String? wxUnionId;
  // final String? wxOpenId;

  Sms({
    this.countryCode,
    this.phoneNum,
    this.appID,
    this.sendTime,
    this.token,
  });

  factory Sms.fromJson(Map<String, dynamic> json) => Sms(
    countryCode: json["countryCode"],
    phoneNum: json["phoneNum"],
    appID: json["appID"],
    sendTime: json["sendTime"],
    token: json["token"],
  );

  Map<String, dynamic> toJson() => {
    "countryCode": countryCode,
    "phoneNum": phoneNum,
    "appID": appID,
    "sendTime": sendTime,
    "token": token,
  };



  // var secret = "abc123!!";
  // var md5 = new MD5CryptoServiceProvider();
  // var tempToken = PhoneNum + VerifyCode + appid + secret + sendtime;
  // var hs = md5.ComputeHash(Encoding.UTF8.GetBytes(tempToken));
  // var tk = BitConverter.ToString(hs).Replace("-", "");
  static String generateToken(String phoneNum, String verifyCode, String appId, String sendTime) {
    // {
    //   "countryCode": "86",
    //   "phoneNum": "18123456789",
    //   "wxUnionId": "",
    //   "wxOpenId": "",
    //   "appID": "com.ods.digital",
    //   "sendTime": "20240102041536",
    //   "token": ""
    // }
    //
    // 18123456789com.ods.digitalabc123!!20240102041536
    // 243 115 10 86 144 157 249 122 233 99 208 67 224 190 166 159
    // F3730A56909DF97AE963D043E0BEA69F
    String secret = "abc123!!";
    // String appId = "com.ods.digital";
    // String sendTime = "20240102041536";


    String tempToken = '$phoneNum$verifyCode$appId$secret$sendTime';
    LogUtils.v("【UTC】(原)字符串= ${tempToken}");

    // String tempToken = '18123456789com.ods.digitalabc123!!20240102041536';
    Uint8List utf8EncodedToken = utf8.encode(tempToken);
    LogUtils.w("【Token】字符串 -> UTF8= ${utf8EncodedToken}");

    // Use md5 package for MD5 hashing
    Digest digest = md5.convert(utf8EncodedToken);
    LogUtils.w("【Token】UTF8 -> MD5= ${digest}");

    String tk = uint8ToHex(digest.bytes);//List<int>
    LogUtils.w("【Token】MD5的bytes -> Hex= ${tk}");
    LogUtils.w("【Token】Token= ${tk}");

    return tk;
  }

  static String uint8ToHex(List<int> byteArr) {
    if (byteArr == null || byteArr.length == 0) {
      return "";
    }
    Uint8List result = Uint8List(byteArr.length << 1);
    var hexTable = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; //16进制字符表
    for (var i = 0; i < byteArr.length; i++) {
      var bit = byteArr[i]; //取传入的byteArr的每一位
      var index = bit >> 4 & 15; //右移4位,取剩下四位
      var i2 = i << 1; //byteArr的每一位对应结果的两位,所以对于结果的操作位数要乘2
      result[i2] = hexTable[index].codeUnitAt(0); //左边的值取字符表,转为Unicode放进resut数组
      index = bit & 15; //取右边四位
      result[i2 + 1] = hexTable[index].codeUnitAt(0); //右边的值取字符表,转为Unicode放进resut数组
    }
    return String.fromCharCodes(result); //Unicode转回为对应字符,生成字符串返回
  }

}